//
//  BoZhuViewController.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/14.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "BoZhuViewController.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
@interface BoZhuViewController ()<UITableViewDataSource, UITableViewDelegate>

@end
//http://mobile.ximalaya.com/mobile/others/ca/album/track/290488/true/1/30?device=iPhone
@implementation BoZhuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:TRUE animated:NO];
    
//    self.navigationController.delegate = self;
    
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height   -44) style:UITableViewStylePlain];
        
        [self.view addSubview:_table];
        self.table.delegate = self;
        self.table.dataSource = self;
//        [self creatHeader];
        _stretchView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 150)];
       
   
        _stretchableTableHeaderView = [HFStretchableTableHeaderView new];
        [_stretchableTableHeaderView stretchHeaderForTableView:self.table withView:_stretchView];
        [self getData];

        
    }
    [self CreatTabbar];

    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];

    // Do any additional setup after loading the view.
}

-(void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone", _MyId];
    
    GetData *data = [[GetData alloc]init];
    [data getDataURL:str dic:nil block:^(id result) {
        NSDictionary *dic = [result objectForKey:@"album"];
        NSDictionary *dic1 = [result objectForKey:@"tracks"];
        
        _informationModel = [[DynamicIFOAlbumModel alloc]init];
        [_informationModel setValuesForKeysWithDictionary:dic];
        
         [_stretchableTableHeaderView setModel:_informationModel];
         [_stretchView  sd_setImageWithURL:[NSURL URLWithString:_informationModel.coverLarge] placeholderImage:[UIImage imageNamed:@"bg_albumView_header@2x.png"]];
        
        
        [_stretchableTableHeaderView.personNameButton addTarget:self action:@selector(visitBozhu:) forControlEvents:UIControlEventTouchUpInside];
        
        self.DATA = [NSMutableArray array];
        NSArray *arr = [dic1 objectForKey:@"list"];
        for (NSDictionary *dic2 in arr) {
            _model = [[SortingModel alloc]init];
            [_model setValuesForKeysWithDictionary:dic2];
            [_DATA addObject:_model];
        }
        [_table reloadData];

        [self.mbview removeFromSuperview];

    }];
    
    
}
-(void)visitBozhu:(UIButton *)sender
{
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_stretchableTableHeaderView scrollViewDidScroll:scrollView];
}
- (void)viewDidLayoutSubviews
{
    [_stretchableTableHeaderView resizeView];
}


//-(void)creatHeader
//{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
//    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
//    left.frame = CGRectMake(5, 0, 100, 30);
//    left.backgroundColor = [UIColor redColor];
//    [view addSubview:left];
//    UIButton *center = [UIButton buttonWithType:UIButtonTypeCustom];
//    center.frame = CGRectMake(110, 0, 100, 30);
//    center.backgroundColor = [UIColor yellowColor];
//    [view addSubview:center];
//    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
//    right.frame = CGRectMake(215, 0, 100, 30);
//    right.backgroundColor = [UIColor greenColor];
//    [view addSubview:right];
//    
//    
//    self.table.tableHeaderView = view;
//    
//    
//}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)popAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    BoZhuSection *view = [[BoZhuSection alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
    

    
    
    
//    [view addSubview:seg];
    return view;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _DATA.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *bozhu = @"Bozhu";
    
    SortingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bozhu];
    if (!cell) {
        cell = [[SortingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:bozhu];
    }
    [cell setModel:_DATA[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    PlayViewController *vc = [[PlayViewController alloc]init];
//    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    

//    vc.model = _DATA[indexPath.row];
    vc.DataSource = _DATA;
    vc.num = [NSString stringWithFormat:@"%ld",indexPath.row];
//    [self.navigationController pushViewController:vc animated:YES];
    [self presentViewController:vc animated:YES completion:nil];
    

}
-(void)CreatTabbar
{
    self.downView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44, [UIScreen mainScreen].bounds.size.width , 44)];
    _downView.backgroundColor = [UIColor blackColor];

    
    [self.view addSubview:_downView];
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame =  CGRectMake(10, 10, 24, 24);
    
    [self.backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [self.downView addSubview:_backButton];
    self.popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.popButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 35, 10, 24, 24);
    
    [self.popButton addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.popButton setImage:[UIImage imageNamed:@"iconfont-home.png"] forState:UIControlStateNormal];
    
    [self.downView addSubview:_popButton];
    
    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(64 * 2, 568 - 64, 64, 64)];
    [self.view addSubview:black];
    black.layer.cornerRadius = 32;
    black.clipsToBounds = YES;
    
    black.backgroundColor = [UIColor blackColor];
    
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.play.frame = CGRectMake(0, 0, 60, 60);
    self.play.center = black.center;
    
    [self.view addSubview:self.play];
    
    self.play.layer.cornerRadius = 30;
    [self.view bringSubviewToFront:self.play];
    self.play.clipsToBounds = YES;
    [self.play addTarget:self action:@selector(GotoPlay) forControlEvents:UIControlEventTouchUpInside];
    
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    
    

}

-(void)viewWillAppear:(BOOL)animated
{
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    _Playmodel = audioManager.Mymodel;
    
    
    if (audioManager.flag) {
        
        if (!_Timer) {
            //            self.Timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
            
            self.Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
        }
        
        //将定时器加入循环
        //循环用于系统页面响应,定时器和进程
        //                [[NSRunLoop currentRunLoop] addTimer:_Timer forMode:NSDefaultRunLoopMode];
        
        
        [_Timer setFireDate:[NSDate distantPast]];
        
        
        
        
    }
    else
    {
        
        [_Timer setFireDate:[NSDate distantFuture]];
        
        //        if ([_Timer isValid]) {
        //            永久停止
        //            [self.Timer invalidate];
        //            _Timer =nil;
        //        }
        
        
    }
    
}
-(void)zhuan
{
    self.play.transform = CGAffineTransformRotate(self.play.transform,2 * M_PI / 60.0 /100);
    //    if (_model.coverLarge) {
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    //    }
}

-(void)GotoPlay
{
    PlayViewController *vc = [[PlayViewController alloc]init];
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    vc.model = audioManager.Mymodel;
    vc.DataSource = audioManager.Mydata;
    /**
     *  number不记录
     */
    
    //    vc.number = audioManager.num;
    vc.num = [NSString stringWithFormat:@"%ld",audioManager.num];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [self.Timer invalidate];
    _Timer =nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//
//    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];
//    
//    self.navigationController.navigationBar.alpha = 0.300;
//    self.navigationController.navigationBar.translucent = YES;
//
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
